package com.sam.im.heiqun.utils;

import android.os.Environment;

import com.sam.im.heiqun.app.App;

import java.io.File;

/**
 * Created by zhangqiang on 2018/7/12.
 */
public class PathUtil {

    private static File baseDir;

    static {

        if (isSdCardAvaliable()) {
            baseDir = new File(Environment.getExternalStorageDirectory(), App.getInstance().getPackageName());
        } else {
            baseDir = App.getInstance().getFilesDir();
        }
        mkdirsWithThrowable(baseDir);
    }


    public static boolean isSdCardAvaliable() {
        return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
    }

    public static File getVideoDir(){
        return mkdirsWithThrowable(new File(baseDir,"video"));
    }

    private static File mkdirsWithThrowable(File dir) {

        if (dir == null) {
            throw new NullPointerException("dir null");
        }

        if (dir.exists()) {
            return dir;
        }
        boolean successful = dir.mkdirs();
        if (!successful) {
            throw new RuntimeException("cannot create dir : " + dir.getAbsolutePath());
        }
        return dir;
    }

    public static File getImageDir() {
        return mkdirsWithThrowable(new File(baseDir,"image"));
    }
}
